<?php

namespace App\Model;

/**
 * Class User_address
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/30 0030
 * @Time 14:51
 */
class User_address extends Common
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = 'user_address_id';
    /**
     * @var string
     */
    protected $table = 'user_address';

    /**
     * 方法 : add_user_address
     * @User wuqin
     * @DateTime 2018-03-22 13:36
     * @param $var
     * @return bool
     */
    public function add_user_address($var)
    {

        $this->user_id = $var['user_id'];
        $this->province = $var['province'];
        $this->city = $var['city'];
        $this->county = $var['county'];
        $this->address = $var['address'];
        $this->phone = $var['phone'];
        $this->get_name = $var['get_name'];

        if ($this->is_default != $var['is_default']) {
            $this->del_redis("user_address:" . $this->user_address_id . ":u:" . $this->user_id . ":is:" . $this->is_default);
        }
        $this->is_default = $var['is_default'];

        if (!$this->save())
            return false;
        $where = array(
            array(
                'where', 'user_address_id', '=', $this->user_address_id
            )
        );
        $data = $this->get($where, '*', 0, 0);
        if (empty($data)) {
            return false;
        }
//        $var['user_address_id'] = $this->user_address_id;
//        $data[0] = $var;
        $this->set_array($data);
        if (!empty($this->exists_redis("user_address:" . $this->user_address_id . ":u:" . $this->user_id . ":is:" . $this->is_default))) {
            return $this;
        }
        return false;
    }

    /**
     * 方法 : set_array
     * @User wuqin
     * @DateTime 2018-03-29 20:37
     * @param $data
     * @return bool
     */
    public function set_array($data)
    {
        if (empty($data))
            return false;
        foreach ($data as $key => $value) {
            $this->set_array_redis('user_address:' . $value['user_address_id'] . ':u:' . $value['user_id'] . ':is:' . $value['is_default'], $value);
        }
    }

    /**
     * 方法 : update_user_address
     * @User wuqin
     * @DateTime 2018-03-22 13:36
     * @param $var
     * @return bool
     */
    public function update_user_address($var)
    {
        $this->province = $var['province'];
        $this->city = $var['city'];
        $this->county = $var['county'];
        $this->address = $var['address'];
        $this->phone = $var['phone'];
        $this->get_name = $var['get_name'];
        $this->is_default = $var['is_default'];
        if (!$this->save())
            return false;
        $where = array(
            array(
                'where', 'user_address_id', '=', $this->user_address_id
            )
        );
        $data = $this->get($where, '*', 0, 0);
        if (empty($data)) {
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->exists_redis("user_address:" . $this->user_address_id . ":u:" . $this->user_id . ":is:" . $this->is_default))) {
            return $this;
        }
        return false;
    }

    /**
     * 方法 : get_all_user_address
     * @User wuqin
     * @DateTime 2018-03-29 20:37
     * @return array|mixed
     */
    public function get_all_user_address()
    {
        $key_index = 'user_address:*';
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get(array(), '*', 0, 0, ['user_id', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('user_address:0');
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_user_address_by_user_id
     * @User wuqin
     * @DateTime 2018-03-29 20:37
     * @param $user_id
     * @return array|mixed
     */
    public function get_user_address_by_user_id($user_id)
    {
        $key_index = 'user_address:*:u:' . $user_id . ':is:*';
        if (empty($keys = $this->keys_redis($key_index))) {
            $where = array(
                array(
                    'where', 'user_id', '=', $user_id
                )
            );
            $data = $this->get($where, '*', 0, 0);
            if (empty($data))
                $this->set_empty_array_redis('user_address:0:u:' . $user_id . ':is:*');
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_default_user_address_by_user_id
     * @User wuqin
     * @DateTime 2018-03-30 14:51
     * @param $user_id
     * @return array|bool|mixed
     */
    public function get_default_user_address_by_user_id($user_id)
    {
        $key_index = 'user_address:*:u:' . $user_id . ':is:1';
        if (empty($keys = $this->keys_redis($key_index))) {
            $where = array(
                array(
                    'where', 'user_id', '=', $user_id
                ),
                array(
                    'where', 'is_default', '=', 1
                )
            );
            $data = $this->get($where, '*', 1, 0);
            if (empty($data))
                return false;
            $this->set_array($data);
            return $data;
        }
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : set_empty
     * @User wuqin
     * @DateTime 2018-03-29 20:37
     * @param string $user_address_id
     * @param string $user_id
     */
    public function set_empty($user_address_id = '0', $user_id = '0')
    {
        $this->set_empty_array_redis('user_address:' . $user_address_id . ':u:' . $user_id . ':is:*');
    }

    public function user()
    {
        return $this->belongsTo(User::class, "user_id");
    }
}
